var express = require('express'); //引入构建服务器的框架
var express = express(); //实例化服务器
var cheerio = require('cheerio'); //引入dom操作库
var request = require('request'); //引入可访问其他网站的库
const fs = require('fs');
var data;
var fuc_json_get_info = function () {
	var cheerio = require('cheerio'); //引入dom操作库
	var request = require('sync-request'); //引入可访问其他网站的库
	var res = request('GET', 'http://career.job.sdu.edu.cn/eweb/jygl/index.so?modcode=null&subsyscode=zpfw&type=ssoSearchZxzp&xxlb=5100');

	//将页面加载到选择器 当前$是选择器
	var $ = cheerio.load(res.getBody());
	console.log('body载入完成' + $('.moreList>div>ul>li>div:first-child>a').length);
	//获取发布日期、单位名单和网址 json版
	var json_items = {};

	for (var i = 0; i < $('.moreList>div>ul>li>div:first-child>a').length; i++) {

		console.log('第' + i + '个公司生成中-----------------------------------');

		var str_company_name = $('.moreList>div>ul>li>div:nth-child(2)>a').eq(i).text().substring(0, $('.moreList>div>ul>li>div:nth-child(2)>a').eq(i).text().lastIndexOf('招'));
		//添加公司信息
		var str_url = 'http://career.job.sdu.edu.cn/eweb/jygl/index.so?modcode=jygl_zpxxck&subsyscode=zpfw&rklx=jyw&lmxhV=0402&type=ssoZxzpView&id=' + $('.moreList>div>ul>li>div:nth-child(2)>a').eq(i).attr('onclick').substring(10, 32);
		if (i === 0) {
			data = $('.moreList>div>ul>li>div:first-child>a').eq(i).text();
			console.log(data);
			if (fs.existsSync(`./test-${data}.txt`) === true) {
				res.download(`./test-${data}.txt`);
				console.log('send.');
				return 0;
			}
		}
		console.log('即将添加第' + i + '个公司的初级信息');
		json_items[str_company_name] = {
			'str_url': str_url,
			'str_pub_data': $('.moreList>div>ul>li>div:first-child>a').eq(i).text(),
			'str_ddl': $('.moreList>div>ul>li>div:nth-child(4)>a').eq(i).text()
		};
		console.log('第' + i + '个公司的初级信息添加完成');



		console.log('即将添加第' + i + '个公司的详细信息');


		var res_in = request('GET', str_url);
		var $$ = cheerio.load(res_in.getBody());
		json_items[str_company_name]['company_website'] = $$('#zpxxtab>tbody>tr:nth-child(2)>td:nth-child(2)').text();
		console.log('公司的详细信息添加完成' + $$('#tab').length);
		json_items[str_company_name]['job'] = {}
		for (var ii = 0; ii < $$('#tab').length; ii++) {
			console.log(ii + '个岗位')
			json_items[str_company_name]['job'][$$('#tab>tbody>tr:nth-child(1)>td:nth-child(2)').eq(ii).text().trim()] = {
				'number_of_people': $$('#tab>tbody>tr:nth-child(2)>td:nth-child(2)').eq(ii).text().trim(),
				'location': $$('#tab>tbody>tr:nth-child(2)>td:nth-child(4)').eq(ii).text().trim(),
				'language': $$('#tab>tbody>tr:nth-child(3)>td:nth-child(2)').eq(ii).text().trim(),
				'pay': $$('#tab>tbody>tr:nth-child(3)>td:nth-child(4)').eq(ii).text().trim(),
				'education_background': $$('#tab>tbody>tr:nth-child(4)>td:nth-child(2)').eq(ii).text().trim(),
				'major': $$('#tab>tbody>tr:nth-child(5)>td:nth-child(2)').eq(ii).text().trim(),
				'description': $$('#tab>tbody>tr:nth-child(6)>td:nth-child(2)').eq(ii).text().trim(),
			};
			console.log('完成第' + ii + '个公司的岗位信息');

		}
		console.log('第' + i + '个公司添加完成')
	}
	return json_items;
}



var app = express.get('/', function (req, res) {

	var json_items = fuc_json_get_info();
	var f = JSON.stringify(json_items);
	fs.writeFile(`./test-${data}.txt`, f, function (err) {
		if (err) {
			throw err;
		}
		res.download(`./test-${data}.txt`);
		console.log('send.');
	});

})
var server = express.listen(3000, function () {
	var host = server.address().address;
	var port = server.address().port;
	console.log('Example app listening at http://%s:%s', host, port);
});